package com.tensquare.user.controller;

import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import utils.JwtUtil;

import javax.naming.ldap.HasControls;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WF on 2020/7/25 15:25
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;
    //1.发送手机验证码
    @PostMapping("sendsms/{mobile}")
    public Result sendSms(@PathVariable String mobile){
        //1.1)发送手机验证码
        userService.sendSms(mobile);
        //1.2)返回
        return new Result(true, StatusCode.OK,"发送手机验证码成功！");
    }
    //2.注册用户
    @PostMapping("register/{code}")
    public Result register(@PathVariable String code,@RequestBody User user){
        //2.1)将用户输入的验证码与redis中保存的验证码进行比对
        if(userService.isValid(code,user.getMobile())){
            userService.add(user);
            return new Result(true,StatusCode.OK,"添加用户成功！");
        }else{
            return new Result(false,StatusCode.ERROR,"验证码输入有误！");
        }
    }
    //3.添加用户
    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return new Result(true,StatusCode.OK,"添加用户成功！");
    }
    //4.用户登录
    @PostMapping("login")
    public Result login(@RequestBody User user){
        User loginUser = userService.login(user);
        //生成token信息
        String token = jwtUtil.createJWT(loginUser.getId(), loginUser.getNickname(), "user");
        Map map = new HashMap<>();
        map.put("name",loginUser.getNickname());
        map.put("token",token);
        if(loginUser != null){
            return new Result(true,StatusCode.OK,"用户登录成功！",map);
        }
        return new Result(false,StatusCode.LOGINERROR,"用户登录失败！",map);
    }
    //5.删除用户
    @DeleteMapping("{userId}")
    public Result delete(@PathVariable String userId){
        //5.1)从请求作用域中得到claims对象
        Claims claims = (Claims) request.getAttribute("claims");
        System.out.println("claims = " + claims);
        //5.2)判断是否存在
        if(claims != null){
            //5.3)得到用户角色
            String roles = (String) claims.get("roles");
            //5.4)判断角色是否存在
            if(!StringUtils.isEmpty(roles) && "admin".equals(roles)){
                userService.delete(userId);
                return new Result(true,StatusCode.OK,"删除用户成功!");
            }
        }
        return new Result(false,StatusCode.ACCESSERROR,"没有管理员权限!");
    }
    //6.增加粉丝数
    @PostMapping("/incfans/{userid}/{x}")
    public Result incfans(@PathVariable String userid,@PathVariable int x){
        userService.incfans(userid,x);
        return new Result(true,StatusCode.OK,"增加粉丝数成功！");
    }
    //7.增加关注数
    @PostMapping("/incfollow/{userid}/{x}")
    public Result incFollow(@PathVariable String userid,@PathVariable int x){
        userService.incfollow(userid,x);
        return new Result(true,StatusCode.OK,"增加关注数成功！");
    }
}
